#include "GameScene.h"
//-----------------------------ItemLayer---------------------
bool isItemTouching;

GItemLayer::GItemLayer()
{
	this->setTouchEnabled(true);
}

bool  GItemLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());

		CCSprite *speedSprite =CCSprite::create("CloseNormal.png");
		speedSprite->setAnchorPoint(ccp(0,0));
		speedSprite->setPosition(ccp(0,speedSprite->getContentSize().height));
		this->addChild(speedSprite,1,1);

		bRet = true;
	} while (0);

	return bRet;
}


void GItemLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* pTouch = (CCTouch*)(*it);
	CCPoint touchLocation = pTouch->getLocationInView();
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
	CCRect rect =	this->getChildByTag(1)->boundingBox();
	bool istouch = rect.containsPoint(touchLocation);
	if(istouch)
	{
		isItemTouching =true;
		GameLayer *layer =GameLayer::sharedGameLayer();
		layer->changeSpeed(true);
	}
	else
	{
		if(isItemTouching)
		{
			GameLayer *layer =GameLayer::sharedGameLayer();
			layer->changeSpeed(false);
		}
	}

}

void GItemLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* pTouch = (CCTouch*)(*it);
	CCPoint touchLocation = pTouch->getLocationInView();
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
	CCRect rect =	this->getChildByTag(1)->boundingBox();
	bool istouch = rect.containsPoint(touchLocation);
	if(istouch)
	{
		isItemTouching =true;
		GameLayer *layer =GameLayer::sharedGameLayer();
		layer->changeSpeed(true);
	}
	else
	{
		if(isItemTouching)
		{
			GameLayer *layer =GameLayer::sharedGameLayer();
			layer->changeSpeed(false); 
		}
	}

}

void GItemLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	if(isItemTouching)
	{
		GameLayer *layer = GameLayer::sharedGameLayer();
		layer->changeSpeed(false);
	}
	isItemTouching =false;
}
